//
//  DYJDateListContentViewController.m
//  Piece
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "DYJDateListContentViewController.h"
#import "DYJDateListTableViewCell.h"
#import "DYJShortDateListViewController.h"
#import "DYJSerialDateListViewController.h"
#import "DYJQuestionDateListViewController.h"

@interface DYJDateListContentViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSString *year;
@property (nonatomic, strong) NSString *month;
@property (nonatomic, assign) NSInteger yearNum;
@property (nonatomic, assign) NSInteger monthNum;
@property (nonatomic, assign) NSInteger shortDateCount;
@property (nonatomic, assign) NSInteger serialDateCount;
@property (nonatomic, assign) NSInteger questionDateCount;
@property (nonatomic, strong) UITableView *dateTableView;
@property (nonatomic, strong) NSMutableArray *shortDateArray;
@end

@implementation DYJDateListContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getCurrentDate];
    [self createDateTableView];
    [self getDateArray];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
}
#pragma mark -- 获取当前日期
- (void)getCurrentDate
{
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *yearformatter=[[NSDateFormatter alloc] init];
    NSDateFormatter  *monthformatter=[[NSDateFormatter alloc] init];
    
    [yearformatter setDateFormat:@"YYYY"];
    [monthformatter setDateFormat:@"MM"];
    
    _year =[yearformatter stringFromDate:senddate];
    _month =[monthformatter stringFromDate:senddate];
    
    //    NSLog(@"locationString:%@",year);
    //    NSLog(@"locationString:%@",month);

}
#pragma mark -- tableView布局
- (void)createDateTableView
{
    _dateTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 35 - 49 - 64) style:UITableViewStylePlain];
    _dateTableView.delegate = self;
    _dateTableView.dataSource = self;
    [self.view addSubview:_dateTableView];
    
    //注册cell
    [_dateTableView registerClass:[DYJDateListTableViewCell class] forCellReuseIdentifier:@"dateCell"];
}
#pragma mark -- 创建日期数组
- (void)getDateArray
{
    _shortDateArray = [NSMutableArray array];
    _yearNum = [_year integerValue];
    _monthNum = [_month integerValue];
    _shortDateCount = (NSInteger)((_yearNum - 2012) * 12 + _monthNum - 10) + 1;
    _serialDateCount = (NSInteger)((_yearNum - 2016) * 12 + _monthNum - 1) + 1;
    _questionDateCount = (NSInteger)((_yearNum - 2012) * 12 + _monthNum - 10) + 1;
    for (NSInteger i = 0; i < _shortDateCount; i++) {
        NSString *dateStr = [NSString stringWithFormat:@"%ld-%ld", _yearNum, _monthNum];
        [_shortDateArray addObject:dateStr];
        _monthNum --;
        if (_monthNum == 0) {
            _monthNum = 12;
            _yearNum --;
        }
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_pageIndex == 0) {
         return _shortDateCount;
    }
    else if (_pageIndex == 1)
    {
        return _serialDateCount;
    }
    else
    {
        return _questionDateCount;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYJDateListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dateCell"];
    if (indexPath.row == 0) {
        cell.dateLabel.text = @"本月";
        cell.dateLabel.font = [UIFont systemFontOfSize:15];
    }
    else
    {
        cell.dateLabel.text = [_shortDateArray objectAtIndex:indexPath.row];
        cell.dateLabel.font = [UIFont systemFontOfSize:15];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_pageIndex == 0) {
        DYJShortDateListViewController *sdlvc = [[DYJShortDateListViewController alloc] init];
        sdlvc.dateUrl = [_shortDateArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:sdlvc animated:YES];
    }
    else if (_pageIndex == 1) {
        DYJSerialDateListViewController *slvc = [[DYJSerialDateListViewController alloc] init];
        slvc.dateUrl = [_shortDateArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:slvc animated:YES];
    }
    else
    {
        DYJQuestionDateListViewController *qdlvc = [[DYJQuestionDateListViewController alloc] init];
        qdlvc.dateUrl = [_shortDateArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:qdlvc animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
